var date = new Date();
var year = date.getFullYear();
var month = date.getMonth()+1;
var today = date.getDate();
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    Show: "none",

    // 添加病例
    addCasesShow: true,
    cases: [],
    upImgList:[],
    tempimgPath:[]
  },


  onLoad: function(options) {
    var _that = this
    this.casesShow()
  },

  /*添加图片 文件*/
  showList: function(e) {
    var that = this
    var show = "";
    if (this.data.Show == "none") {
      show = "block"
    } else {
      show = "none"
    }
    this.setData({
      Show: show
    })
  },
  // 关闭弹窗
  close: function(e) {
    var hide = "";
    if (this.data.Show == "block") {
      hide = "none"
    } else {
      hide = "none"
    }
    this.setData({
      Show: hide
    })
  },
  //病例展示请求接口
  casesShow: function() {
    var that = this;
    var url = app.util.url('entry/wxapp/cases')
    app.util.request({
      url: url,
      data: {
        m: 'md_daojia',
        op: 'display',
        uid: wx.getStorageSync('userInfo')
      },
      success: function(res) {
        var list = res.data.data
        // for(let i=0;i<list.length;i++){
          
        //   list[i].imgadd.split(',')
          
        //   console.log(list[i].imgadd.split(','))
        // }
        
        // console.log(list + "list===============================")

        that.setData({
          casesList: list
        })
        console.log("病例=========>", res);
      }
    })


  },

  // 获取文字内容
  text: function(e) {
    console.log(e.detail.value)
    this.setData({
      text: e.detail.value
    })
  },
  btnPicture: function () {
    var _that = this
    wx.chooseImage({
      success(res) {
        const tempimgPaths = res.tempFilePaths
        console.log(tempimgPaths)

        _that.setData({
          tempimgPath: tempimgPaths
        })
        console.log("图片地址===========>", _that.data.tempimgPath)
        // s把选择的图片上传到服务器
        let url = app.util.url('entry/wxapp/upload')
        console.log(url)
        wx.uploadFile({
          url: url,
          filePath: tempimgPaths[0],
          name: 'file',
          header: {},
          formData: {
            'user': 'test',
            'm':'md_daojia'
          },
          success: function (res) {
            console.log(res.data)
            _that.data.upImgList.push(res.data)
            
            _that.setData({
              upImgList: _that.data.upImgList
            })
          }
        })




      }
    })
  },

  // 上传图片
  // btnPicture: function() {
  //   var _that = this
  //   wx.chooseImage({
  //     success(res) {
  //       const tempimgPaths = res.tempFilePaths
  //       _that.setData({
  //         tempimgPath: tempimgPaths[0]
  //       })
  //       if (tempimgPaths[0]) {
  //         wx.showToast({
  //           title: '图片上传成功',
  //           icon: "none"
  //         })
  //       }
  //       console.log("图片地址===========>", _that.data.tempimgPath)
  //     }
  //   })
  // },
  // 上传文件
  btnFile: function() {
    var _that = this
    wx.chooseImage({
      success(res) {
        const tempFilePaths = res.tempFilePaths
        _that.setData({
          tempFilePath: tempFilePaths[0]
        })
        if (tempFilePaths[0]) {
          wx.showToast({
            title: '图片上传成功',
            icon: "none"
          })
        }
        console.log("文件地址==========>", _that.data.tempFilePath)
        
      }
    })
  },
  // 确认上传
  btnSubmit: function() {
    var that = this;
    var hide = "";
    
    console.log(that.data)
    var url = app.util.url('entry/wxapp/casesupload')
    app.util.request({
      url: url,
      data: {
        m: 'md_daojia',
        uid: wx.getStorageSync('userInfo'),
        text: that.data.text,
        imadd: that.data.tempimgPath,
        pdfadd: that.data.tempFilePath
      },
      success: function(res) {
        console.log("确认上传=========>", res);
        that.casesShow()
        setInterval(function() {
          if (that.data.Show == "block") {
            hide = "none"
          } else {
            hide = "none"
          }
          that.setData({
            Show: hide
          })

        }, 2000)

      }
    })
  },

  // wang new ============================================================
  // 添加病例
  addCasesBtn: function(t) {
    let that = this
    that.setData({
      addCasesShow: !that.data.addCasesShow
    })
  },

  addCases: function(t) {
    let that = this
    if (that.data.upImgList.length<1){
      wx.showToast({
        title: '未传图片',
      })
      return 
    }
    console.log(t)
    let newAddCases = t.detail.value.newCases
    let newAddcasesTime = year + "年" + month + "月" + today + "日"
    // 这是原来的假数据
    let oldCases = that.data.cases
    // oldCases.push({
    //   time: newAddcasesTime,
    //   upimg: [],
    //   words: newAddCases
    // })
    that.setData({
      addCasesShow: !that.data.addCasesShow,
    })

    var url = app.util.url('entry/wxapp/casesupload')
    app.util.request({
      url: url,
      data: {
        m: 'md_daojia',
        uid: wx.getStorageSync('userInfo'),
        text: newAddCases,
        imadd: that.data.upImgList.toString(),
        // pdfadd: that.data.tempFilePath
      },
      success: function (res) {
        console.log(res)
        wx.startPullDownRefresh({
          success: res => {
            that.casesShow()
          }
        })
      }
    })




  },
  // 取消添加病例
  closeAddCases: function(t) {
    let that = this
    that.setData({
      addCasesShow: !that.data.addCasesShow
    })
  },
  // 不可滑动
  stopSliding: function(e) {
    console.log("stop")
    return
  },
  // 图片预览
  previewImage: function(e) {
    console.log(e.currentTarget.dataset)
    var current = e.target.dataset.src;
    let that = this
    wx.previewImage({
      current: e.currentTarget.dataset.imglist, // 当前显示图片的http链接  
      urls: that.data.upImgList // 需要预览的图片http链接列表  
    })
  },

  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})